windowloads()
getname ()

function getname (){
var username = getCookie('username')  

if(username){
$('.login-rge')[0].outerHTML= `
<li  class="login-rge">
 <a  href="javascript:void(0);">
 <span>${username}</span></a>
 <a class = "del" onclick = outlogin()>退出</a>
</li>
 `
}
}  

function outlogin(){
    layer.confirm('你确定要退出吗？',{
        btn:['退出','取消']      
        },function(){
        location.href='/';
        removeCookie('username')
        removeCookie('username')
        removeCookie('userid')
        localStorage.removeItem('token')
    })
}
var userid = getCookie('userid')  
getdata()

// 用户头像修改
$('#headupload').on('click',function(){
    $('.upload')[0].click()
    $('.upload').on('change', async  function(){
    const body = new FormData()
    const file = $('.upload')[0].files[0]
    if (file) {
        body.append('image', file)
        let y = /(\.png|\.gif|\.jpg)$/i 
        if(!y.test(file.name)){
            layer.msg('修改失败，请确认需要上传的文件类型是否为png | gif | jpg 格式', {
                icon: 2,
                time: 2000
            })
            return
        }
        $.ajax({
            type: 'post',
            url: '/users/api/userhead/'+userid,
            headers: {
                'Authorization': localStorage.getItem('token')
            },
            contentType:false, 
            processData:false,  
            data:body,
            success:res=>{
                let {
                    errorCode,
                    message
                } = res
                if (errorCode == 0) {
                    getdata()
                    layer.msg(message, {
                        icon: 1,
                        time: 2000
                    })
                } else {
                    layer.msg(message, {
                        icon: 2,
                        time: 2000
                    })
                }
            }
        })
        } else { 
            alert('请选择文件！')
        }
        
    })
}) 

// 获取用户信息
function getdata(){
    $.ajax({
        type: 'get',
        url: '/users/api/list/'+userid,
        headers: {
            'Authorization': localStorage.getItem('token')
        },
        success: res=> {
            if(res.data.userInfo.url){
                $("#headupload img")[0].src = res.data.userInfo.url
            }else{
                $("#headupload img")[0].src = 'imges/f790b51a76afd7b41522048fa779d69d.jpg'
            }
    
            var username = document.querySelector('.user h3')
            var usertel = document.querySelector('.user-tel i')
            var userEmail = document.querySelector('.user-tel p span')
            username.innerText = res.data.userInfo.username
            var stremail = res.data.userInfo.email
            var str11 = stremail.substring(3,7)
            var str22 = stremail.replace(str11,'****')
            userEmail.innerText = str22

            var str = (res.data.userInfo.tel).toString()
            var str1 = str.substring(3,7)
            var str2 = str.replace(str1,'*****')
            usertel.innerText = str2
        
            var date = new Date()
            var Hours = date.getHours()
            var atr
            if(Hours>6&&Hours<11){
             atr = '早上好！'
            }else if(Hours>=11&&Hours<13){
                atr = '中午好！'
            }else if(Hours>=13&&Hours<19){
                atr = '下午好！'
            }else if(Hours>=19&&Hours<=24){
                atr = '晚上好！'
            }else{
                atr = '寄了兄弟们'
            }
            $('.user span')[0].innerText = atr
        }
    })
}